/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2013 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague.forums;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.huguesjohnson.retroleague.StreamEntry;
import com.huguesjohnson.retroleague.StreamEntry.Sources;

public class ForumPostParserHandler extends DefaultHandler{
	private final static String TAG_ENTRY="entry";
	private final static String TAG_AUTHOR="author";
	private final static String TAG_NAME="name";
	private final static String TAG_UPDATED="updated";
	private final static String TAG_LINK="link";
	private final static String TAG_TITLE="title";
	private final static String TAG_CONTENT="content";
	private ArrayList<StreamEntry> forumPosts=new ArrayList<StreamEntry>();
	private StreamEntry currentPost=null;
	private StringBuilder characters=new StringBuilder();
	private boolean readThis=false;
	private static DateFormat dateFormat=null;
	private Date minimumDate;
    private static String bgColor="#000011";
    private static String fontColor="#ffffff";

    public ForumPostParserHandler(Date minimumDate){
    	super();
    	this.minimumDate=minimumDate;
		synchronized(this){
			dateFormat=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
    	}
    }
    
	public ArrayList<StreamEntry> getForumPosts(){
		return(this.forumPosts);
	}
	
	@Override
	public void startDocument() throws SAXException{
		super.startDocument();
	}

    @Override
    public void characters(char[] ch,int start,int length) throws SAXException{
        super.characters(ch,start,length);
        if(readThis){
        	this.characters.append(ch,start,length);
    	}
    }
	
	@Override
	public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException{
		super.startElement(uri,localName,qName,attributes);
		String tagName=localName;
		if((tagName==null)||(tagName.length()<1)){
			tagName=qName;
		}
		if(tagName.equalsIgnoreCase(TAG_ENTRY)){
			this.currentPost=new StreamEntry();
			this.currentPost.setSource(Sources.Forums);
		}
		if(this.currentPost!=null){
			if(tagName.equalsIgnoreCase(TAG_LINK)){
				this.currentPost.setUrl(attributes.getValue("href"));
			}else if(tagName.equalsIgnoreCase(TAG_AUTHOR)){
				readThis=true;
				this.characters.setLength(0);
			}else if(tagName.equalsIgnoreCase(TAG_NAME)){
				readThis=true;
				this.characters.setLength(0);
			}else if(tagName.equalsIgnoreCase(TAG_CONTENT)){
				readThis=true;
				this.characters.setLength(0);
			}else if(tagName.equalsIgnoreCase(TAG_TITLE)){
				readThis=true;
				this.characters.setLength(0);
			}else if(tagName.equalsIgnoreCase(TAG_UPDATED)){
				readThis=true;
				this.characters.setLength(0);
			}
		}
	}

	@Override
	public void endElement(String uri,String localName,String qName) throws SAXException{
		super.endElement(uri,localName,qName);
		String tagName=localName;
		if((tagName==null)||(tagName.length()<1)){
			tagName=qName;
		}
		if(this.currentPost!=null){
			if(tagName.equalsIgnoreCase(TAG_ENTRY)){
				if((this.currentPost.getPostedDate()!=null)&&(this.currentPost.getPostedDate().compareTo(this.minimumDate)>0)){
					this.forumPosts.add(this.currentPost);
				}
			}else if(tagName.equalsIgnoreCase(TAG_AUTHOR)){
				this.currentPost.setAuthor(this.characters.toString());
				readThis=false;
			}else if(tagName.equalsIgnoreCase(TAG_NAME)){
				this.currentPost.setTitle(this.characters.toString());
				readThis=false;
			}else if(tagName.equalsIgnoreCase(TAG_CONTENT)){
				StringBuffer content=new StringBuffer(this.characters.toString());
				if(content.substring(0,1).equals("\\n")){
					content.delete(0,1);
				}
				content.insert(0,"<p>");
				int indexOf=content.indexOf("<p>",4);
				if(indexOf>=4){
					content.insert(indexOf,"</p>");
				}else{
					content.append("</p>");
				}
				indexOf=content.indexOf("<hr />");
				if(indexOf>0){
					content.delete(indexOf,indexOf+6);
				}
				content.insert(0,"'>");
				content.insert(0,fontColor);
				content.insert(0,"'><font color='");
				content.insert(0,bgColor);
				content.insert(0,"<html><body bgcolor='");
				content.append("</font></body></html>");
				this.currentPost.setContent(content.toString());
				readThis=false;
			}else if(tagName.equalsIgnoreCase(TAG_TITLE)){
				this.currentPost.setTitle(this.characters.toString());
				readThis=false;
			}else if(tagName.equalsIgnoreCase(TAG_UPDATED)){
				String dateString=this.characters.toString();
				try{
					Date date=dateFormat.parse(dateString);
					this.currentPost.setPostedDate(date);
				}catch(Exception x){
					this.currentPost.setPostedDate(null);
				}
				readThis=false;
			}
		}
	}
}